﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using OA.BLL;

using OA.Model;
using OA.Web.Areas.Member.Controllers;
using OA.Web.Attribute;
using OA.Web.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace OA.Web.Areas.Management.Controllers
{
    [Area("Management")]
    public class EmployeePositionController : BaseMemberController
    {
        private IMemoryCache _cache;
        public EmployeePositionController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
        }

        [MyActionFilter("205004", "职员岗位", "人力资源管理", true, "iconzaizhiyuangong")]
        public IActionResult Index(string empID)
        {
            ViewBag.EMpID = empID;
            return View();
        }

        [HttpGet]
        [MyActionFilter("205004", "职员岗位")]
        public IActionResult GetData(string empID, int page = 1, int limit = 12)
        {
            var dbContext = this.dbContextFactory.DbContext;
            var departments=dbContext.Department.Select(SimpleDepartment.Map).ToList();
            if(string.IsNullOrWhiteSpace(empID))
            {
                var infoList = from employeePosition in dbContext.EmployeePosition
                             where employeePosition.Status >= 0
                             join position in dbContext.Position
                                on employeePosition.PositionID equals position.ID into dc
                             from dci in dc.DefaultIfEmpty()
                             join department in dbContext.Department
                             on dci.DepartmentID equals department.ID into ec
                             from eci in ec.DefaultIfEmpty()
                             select new MyPositions2
                             {
                                 ID = employeePosition.ID,
                                 DepartmentID = eci.ID,                                 
                                 Name = dci.Name,
                                 StartTime = employeePosition.StartTime,
                                 EndTime = employeePosition.EndTime,
                                 Status= employeePosition.Status,
                                 EmpID= employeePosition.EmpID

                             };
                int count = infoList.Select(m => m.ID).Count();
                var list= infoList.OrderByDescending(m => m.ID).Skip((page - 1) * limit).Take(limit).ToList();
                foreach (var item in list)
                {
                    item.DepartmentName = CDepartment.GetFullDepartmentNameByID(item.DepartmentID, departments);
                }
                var result = new
                {
                    code = "0",
                    msg = "",
                    count = count,
                    data = list
                };
                return Json(result);
            }
            else
            {
                var infoList = from employeePosition in dbContext.EmployeePosition
                             where employeePosition.Status >= 0 && employeePosition.EmpID == empID
                             join position in dbContext.Position
                                on employeePosition.PositionID equals position.ID into dc
                             from dci in dc.DefaultIfEmpty()
                             join department in dbContext.Department
                             on dci.DepartmentID equals department.ID into ec
                             from eci in ec.DefaultIfEmpty()
                             select new MyPositions2
                             {
                                 ID = employeePosition.ID,
                                 DepartmentID = eci.ID,                                 
                                 Name = dci.Name,
                                 StartTime = employeePosition.StartTime,
                                 EndTime = employeePosition.EndTime,
                                 Status = employeePosition.Status,
                                 EmpID = employeePosition.EmpID
                             };

                int count = infoList.Select(m => m.ID).Count();
                var list = infoList.OrderByDescending(m=>m.ID).Skip((page - 1) * limit).Take(limit).ToList();
                foreach (var item in list)
                {
                    item.DepartmentName= CDepartment.GetFullDepartmentNameByID(item.DepartmentID, departments);
                }                
                var result = new
                {
                    code = "0",
                    msg = "",
                    count = count,
                    data = list
                };
                return Json(result);
            }
        }


        [HttpGet]
        [MyActionFilter("205004001", "编辑职员岗位", "职员岗位", false, "")]
        public IActionResult Edit(int id)
        {
            EmployeePosition info= this.GetInfo<EmployeePosition>(id);
            ViewBag.DepartmentID = 0;
            if (info == null)
            {
                ViewBag.Msg = "您编辑的岗位信息不存在";                 
            }
            else
            {
                Position infoPosition = this.GetInfo<Position>(info.PositionID);
                ViewBag.DepartmentID = infoPosition.DepartmentID;
            }            
            return View(info);
        }

        [HttpPost]
        [MyActionFilter("205004001", "编辑职员岗位", "职员岗位", false, "")]
        public IActionResult Edit(EmployeePosition model)
        {
            BaseResult baseResult = new BaseResult();
            EmployeePosition info = this.GetInfo<EmployeePosition>(model.ID);
            if (info == null)
            {
                baseResult.State = 400;
                baseResult.Msg = "您编辑的岗位信息不存在";
                return Json(baseResult);
            }
            else
            {
                string empName=this.dbContextFactory.DbContext.Employee.Where(m => m.EmpID == model.EmpID).Select(m=>m.EmpName).FirstOrDefault();
                model.EmpName = empName;
                this.Update<EmployeePosition>(info, model, null, new string[] { "ID","Status"});
            }
            baseResult.Msg = "当前职员岗位信息修改成功！";
            return Json(baseResult);
        }

        [HttpGet]
        [MyActionFilter("205004002", "删除职员岗位", "职员岗位", false, "")]
        public IActionResult Delete(int id)
        {
            BaseResult baseResult = new BaseResult();
           this.Delete<EmployeePosition>(id);
            return Json(baseResult);
        }
    }
}